package com.hzlj.position.locate.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.DeleteCommonDTO;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.exception.PubException;
import com.fz.common.dict.annotation.DictConvert;
import com.google.common.collect.Maps;
import com.hzlj.position.locate.cache.ReportInitiativeSettingCache;
import com.hzlj.position.locate.common.dto.reportInitiativeSetting.ReportInitiativeSettingAddDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSetting.ReportInitiativeSettingQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSetting.ReportInitiativeSettingQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSettingJzry.ReportInitiativeSettingJzryAmtBySettingIdDTO;
import com.hzlj.position.locate.common.dto.reportInitiativeSettingJzry.ReportInitiativeSettingJzryQueryParamDTO;
import com.hzlj.position.locate.common.model.ReportInitiativeSetting;
import com.hzlj.position.locate.converter.ReportInitiativeSettingConverter;
import com.hzlj.position.locate.dao.ReportInitiativeSettingDao;
import com.hzlj.position.locate.mapper.ReportInitiativeSettingJzryMapper;
import com.hzlj.position.locate.service.ReportInitiativeSettingJzryService;
import com.hzlj.position.locate.service.ReportInitiativeSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.hzlj.position.config.config.IdConfig.POSITION_ID_GEN;

/**
 * 通信联络核查-人脸签到-配置表(ReportInitiativeSetting)表服务接口
 *
 * @author lifh
 * @date 2023-07-20 10:41:38
 */
@Slf4j
@Service
public class ReportInitiativeSettingServiceImpl implements ReportInitiativeSettingService {
    @Resource
    private ReportInitiativeSettingDao reportInitiativeSettingDao;
    @Resource
    private ReportInitiativeSettingCache reportInitiativeSettingCache;
    @Resource
    private ReportInitiativeSettingJzryService reportInitiativeSettingJzryService;
    @Resource
    private ReportInitiativeSettingJzryMapper reportInitiativeSettingJzryMapper;


    /**
     * 新增数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReportInitiativeSettingQueryResultDTO saveReportInitiativeSetting(ReportInitiativeSettingAddDTO dto) {
        ReportInitiativeSetting reportInitiativeSetting = ReportInitiativeSettingConverter.from(dto);
        reportInitiativeSetting.setId(POSITION_ID_GEN.nextIdStr());
        this.reportInitiativeSettingDao.save(reportInitiativeSetting);
        log.info("【新增数据成功】:reportInitiativeSetting={}", reportInitiativeSetting);
        return ReportInitiativeSettingConverter.from(reportInitiativeSetting);
    }

    /**
     * 删除数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteReportInitiativeSetting(DeleteCommonDTO<String> dto) {
        Boolean exists = reportInitiativeSettingJzryService.exists(dto.toIds());
        if (exists) {
            throw new PubException("已经存在关联矫正对象，不能删除");
        }
        boolean result = this.reportInitiativeSettingDao.deleteReportInitiativeSetting(dto);
        if (result) {
            log.info("【删除数据成功】:ReportInitiativeSetting={}", dto);
        }
        return result;
    }

    @Override
    public List<ReportInitiativeSettingQueryResultDTO> getSysReportInitiativeSetting(String jgCode) {
        if (ObjectUtil.isEmpty(jgCode)) {
            return null;
        }
        return this.reportInitiativeSettingCache.getSysReportInitiativeSetting(jgCode);
    }

    /**
     * 查询列表：分页
     */
    @Override
    @DictConvert
    public QueryPageResultDTO<ReportInitiativeSettingQueryResultDTO> pageReportInitiativeSetting(ReportInitiativeSettingQueryParamDTO dto, PageParamDTO page) {
        QueryPageResultDTO<ReportInitiativeSettingQueryResultDTO> result = this.reportInitiativeSettingDao.pageReportInitiativeSetting(dto, page);
        this.fillJzryAmt(result.getList());
        return result;
    }

    private void fillJzryAmt(List<ReportInitiativeSettingQueryResultDTO> settings) {
        if (ObjectUtil.isEmpty(settings)) {
            return;
        }
        List<String> settingIds = settings.stream()
                .map(ReportInitiativeSettingQueryResultDTO::getId).collect(Collectors.toList());
        Map<String, Long> amtBySettingIds = Maps.newHashMap();
        if (ObjectUtil.isNotEmpty(settingIds)) {
            amtBySettingIds = this.reportInitiativeSettingJzryMapper.statBySettingId(new ReportInitiativeSettingJzryQueryParamDTO().setSettingIds(settingIds))
                    .stream().collect(Collectors.toMap(ReportInitiativeSettingJzryAmtBySettingIdDTO::getSettingId, ReportInitiativeSettingJzryAmtBySettingIdDTO::getAmt));
        }
        for (ReportInitiativeSettingQueryResultDTO setting : settings) {
            if (!Boolean.TRUE.equals(setting.getSys())) {
                continue;
            }
            Long amt = reportInitiativeSettingJzryMapper.statSysSettingId(
                    new ReportInitiativeSettingJzryQueryParamDTO().setSettingId(setting.getId()).setSys(setting.getSys()).setJgCode(setting.getJgCode())
            );
            amtBySettingIds.put(setting.getId(), amt + amtBySettingIds.getOrDefault(setting.getId(), 0L));
        }
        for (ReportInitiativeSettingQueryResultDTO setting : settings) {
            setting.setJzryAmt(amtBySettingIds.get(setting.getId()));
        }
    }
}
